﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.ParkManage;
using LJX.SmartPark.Domain.SafeManage;
using LJX.SmartPark.EntityFramework;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.ParkManage
{
    public class CustomerService : ICustomerService
    {
        private readonly IRepository<Customer> _studentService;


        public CustomerService(IRepository<Customer> studentService)
        {
            _studentService = studentService;
        }

        public async Task<Response<bool>> DeleteByIdAsync(Customer input)
        {
            var result = await _studentService.DeleteByIdAsync(input.MapTo<Customer>());
            return Response<bool>.Ok(result > 0);
        }

        public async Task<Response<List<Customer>>> GetCustomerAsync(CustomerForm form)
        {
            var students = await _studentService.GetAllListAsync();
            if (!string.IsNullOrEmpty(form.title))
            {
                students = students.Where(x => x.Title.Contains(form.title)).ToList();
            }
            if (form.typeId != 0)
            {
                students = students.Where(x => x.TypeId == form.typeId).ToList();
            }
            var count = students.Count();
            students = students.Skip((form.pageIndex - 1) * form.pageSize).Take(form.pageSize).ToList();
            var result = students.MapToList<Customer>();
            return Response<List<Customer>>.Ok(result, count);
        }

        public async Task<Response<bool>> PostAddCustomer(Customer input)
        {
            var result = _studentService.InsertAsyncById(input.MapTo<Customer>());

            return Response<bool>.Ok(result.Result > 0);
        }

        public async Task<Response<bool>> PutUpdateCustomer(Customer input)
        {
            var result = _studentService.UpdateAsync(input.MapTo<Customer>());

            return Response<bool>.Ok(result.Result > 0);
        }
    }
}
